package com.me.jingdongshop.ui.activity;

import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.me.common.base.BaseActivity;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.LocationList;

/**
 * Created by ldl on 2017/5/7.
 */

public class SettlementActivity extends BaseActivity {

    public static final int REQUEST_CODE = 0;
    public static final int PAY_REQUEST = 1;
    private ImageButton ibBack; // 结算中心返回
    private TextView tvCommit;       // 提交订单
    private ImageView ivAddressNext;    // 送货地址 ->
    private ImageView ivPaymentNext;    // 支付方式 ->
    private ImageView ivDeliveryNext;   // 送货方式 ->
    private ImageView ivInvoiceNext;    // 索取发票 ->
    private TextView tvPreferential;   // 优惠券信息
    private TextView tvReceiver;
    private TextView tvReceiverPhone;
    private TextView tvReceiverAddress;
    private TextView tvPaymentMode;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_shopcart_settlement;
    }

    @Override
    public void initView() {
        ibBack = (ImageButton) findViewById(R.id.ib_settlement_back);
        tvCommit = (TextView) findViewById(R.id.tv_title_commit);
        ivAddressNext = (ImageView) findViewById(R.id.iv_delivery_address_next);
        ivPaymentNext = (ImageView) findViewById(R.id.iv_payment_mode_next);
        ivDeliveryNext = (ImageView) findViewById(R.id.iv_delivery_mode_next);
        ivInvoiceNext = (ImageView) findViewById(R.id.iv_demand_invoice_next);
        tvPreferential = (TextView) findViewById(R.id.tv_preferential_info);
        tvPaymentMode = (TextView) findViewById(R.id.tv_payment_mode);

        tvReceiver = (TextView) findViewById(R.id.tv_receiver);
        tvReceiverPhone = (TextView) findViewById(R.id.tv_receiver_phone);
        tvReceiverAddress = (TextView) findViewById(R.id.tv_receiver_address);
    }

    @Override
    public void initListener() {
        ibBack.setOnClickListener(this);
        tvCommit.setOnClickListener(this);
        ivAddressNext.setOnClickListener(this);
        ivPaymentNext.setOnClickListener(this);
        ivDeliveryNext.setOnClickListener(this);
        ivInvoiceNext.setOnClickListener(this);
        tvPreferential.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v, int id) {
        switch (id) {
            case R.id.ib_settlement_back:
                finish();   // 无法返回购物车首页,Activity启动模式
                break;
            case R.id.tv_title_commit:
                Intent intent1 = new Intent(this, ShopcartSubmitActivity.class);
                startActivity(intent1);
                break;
            case R.id.iv_delivery_address_next:
                Intent intent2 = new Intent(this, AddressListActivity.class);
                startActivityForResult(intent2, REQUEST_CODE);
                break;
            case R.id.iv_payment_mode_next:
                Intent intent3 = new Intent(this, ShopcartPaymodeActivity.class);
                startActivityForResult(intent3, PAY_REQUEST);
                break;
            case R.id.iv_delivery_mode_next:
                Intent intent4 = new Intent(this, DeliveryModeActivity.class);
                startActivity(intent4);
                break;
            case R.id.iv_demand_invoice_next:
                Intent intent5 = new Intent(this, WriteInvoiceActivity.class);
                startActivity(intent5);
                break;
            case R.id.tv_preferential_info:
                Intent intent6 = new Intent(this, MyCouponActivity.class);
                startActivity(intent6);
                break;
        }
    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {

    }

    @Override
    public void onHttpError(int reqType, String error) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == AddressListActivity.RESULT_CODE && requestCode == REQUEST_CODE){
            LocationList.AddressListBean address = (LocationList.AddressListBean) data.getSerializableExtra("address");
            showAddress(address);
        }
        if(requestCode == PAY_REQUEST && resultCode == ShopcartPaymodeActivity.RESULT_CODE){
            String payMode = data.getStringExtra("payMode");
            tvPaymentMode.setText(payMode);
        }

    }

    private void showAddress(LocationList.AddressListBean address) {
        tvReceiver.setText(address.getName());
        tvReceiverAddress.setText(address.getProvince()+address.getCity()+address.getAddressArea()+address.getAddressDetail());
        tvReceiverPhone.setText(address.getPhoneNumber());
    }
}
